package cn.iocoder.yudao.module.member.controller.admin.score;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.member.controller.admin.score.vo.*;
import cn.iocoder.yudao.module.member.convert.score.ScoreConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.score.ScoreDO;
import cn.iocoder.yudao.module.member.service.score.ScoreService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 用户积分记录")
@RestController
@RequestMapping("/member/score")
@Validated
public class ScoreController {

    @Resource
    private ScoreService scoreService;

    @PostMapping("/create")
    @Operation(summary = "创建用户积分记录")
    @PreAuthorize("@ss.hasPermission('member:score:create')")
    public CommonResult<Long> createScore(@Valid @RequestBody ScoreCreateReqVO createReqVO) {
        return success(scoreService.createScore(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户积分记录")
    @PreAuthorize("@ss.hasPermission('member:score:update')")
    public CommonResult<Boolean> updateScore(@Valid @RequestBody ScoreUpdateReqVO updateReqVO) {
        scoreService.updateScore(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户积分记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:score:delete')")
    public CommonResult<Boolean> deleteScore(@RequestParam("id") Long id) {
        scoreService.deleteScore(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户积分记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:score:query')")
    public CommonResult<ScoreRespVO> getScore(@RequestParam("id") Long id) {
        ScoreDO score = scoreService.getScore(id);
        return success(ScoreConvert.INSTANCE.convert(score));
    }

    @GetMapping("/list")
    @Operation(summary = "获得用户积分记录列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('member:score:query')")
    public CommonResult<List<ScoreRespVO>> getScoreList(@RequestParam("ids") Collection<Long> ids) {
        List<ScoreDO> list = scoreService.getScoreList(ids);
        return success(ScoreConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户积分记录分页")
    @PreAuthorize("@ss.hasPermission('member:score:query')")
    public CommonResult<PageResult<ScoreRespVO>> getScorePage(@Valid ScorePageReqVO pageVO) {
        PageResult<ScoreDO> pageResult = scoreService.getScorePage(pageVO);
        return success(ScoreConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户积分记录 Excel")
    @PreAuthorize("@ss.hasPermission('member:score:export')")
    @OperateLog(type = EXPORT)
    public void exportScoreExcel(@Valid ScoreExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ScoreDO> list = scoreService.getScoreList(exportReqVO);
        // 导出 Excel
        List<ScoreExcelVO> datas = ScoreConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "用户积分记录.xls", "数据", ScoreExcelVO.class, datas);
    }

}
